var animation
const app = getApp()
var alldeta
Page({
  data: {
    menuType: 0,
    begin: null,
    status: 1,
    end: null,
    isVisible: false,
    animationData: {},
    animationAddressMenu: {},
    addressMenuIsShow: false,
    value: [0, 0, 0],
    provinces: [],
    citys: [],
    areas: [],
    province: '',
    city: '',
    area: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var ids = options.ids;
    wx.setStorageSync('ids', ids);
    var that = this;
    var prove = options.prove;
    var city = options.city;
    var dist = options.dist;
    alldeta = prove + city + dist;
    
    wx.request({
      url: 'https://www.cakeyouxuan.com/small/my/address_look',
      data: {
        ids: ids,
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"  //post
      },
      success: function (res) {
        if (alldeta) {
          alldeta = alldeta;
        } else {
          alldeta = res.data.alldeta;
        }
        that.setData({
          list: res.data.list,
          myaddress: alldeta,
        });
      },
    })

  },

  // 显示
  showMenuTap: function (e) {
    console.log('selectState')
    //获取点击菜单的类型 1点击状态 2点击时间 
    var menuType = e.currentTarget.dataset.type
    // 如果当前已经显示，再次点击时隐藏
    if (this.data.isVisible == true) {
      this.startAnimation(false, -200)
      return
    }
    this.setData({
      menuType: menuType
    })
    this.startAnimation(true, 0)
  },
  hideMenuTap: function (e) {
    this.startAnimation(false, -200)
  },
  // 执行动画
  startAnimation: function (isShow, offset) {
    var that = this
    var offsetTem
    if (offset == 0) {
      offsetTem = offset
    } else {
      offsetTem = offset + 'rpx'
    }
    this.animation.translateY(offset).step()
    this.setData({
      animationData: this.animation.export(),
      isVisible: isShow
    })
    console.log(that.data)
  },
  // 选择状态按钮
  selectState: function (e) {
    console.log('selectState1')
    this.startAnimation(false, -200)
    var status = e.currentTarget.dataset.status
    this.setData({
      status: status
    })
    console.log(this.data)
  },
  // 日志选择
  bindDateChange: function (e) {
    console.log(e)
    if (e.currentTarget.dataset.type == 1) {
      this.setData({
        begin: e.detail.value
      })
    } else if (e.currentTarget.dataset.type == 2) {
      this.setData({
        end: e.detail.value
      })
    }
  },
  sureDateTap: function () {
    this.data.pageNo = 1
    this.startAnimation(false, -200)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 点击所在地区弹出选择框
  selectDistrict: function (e) {
    var that = this
    if (that.data.addressMenuIsShow) {
      return
    }
    that.startAddressAnimation(true)
  },
  // 执行动画
  startAddressAnimation: function (isShow) {
    console.log(isShow);
    var that = this
    if (isShow) {
      that.animation.translateY(0 + 'vh').step()
    } else {
      that.animation.translateY(40 + 'vh').step()
    }
    that.setData({
      animationAddressMenu: that.animation.export(),
      addressMenuIsShow: isShow,
    })
  },
  // 点击地区选择取消按钮
  cityCancel: function (e) {
    this.startAddressAnimation(false)
  },
  // 点击地区选择确定按钮
  citySure: function (e) {
    var that = this
    var city = that.data.city
    var value = that.data.value
    that.startAddressAnimation(false)
    // 将选择的城市信息显示到输入框
    var areaInfo = that.data.provinces[value[0]].name + ',' + that.data.citys[value[1]].name + ',' + that.data.areas[value[2]].name
    that.setData({
      areaInfo: areaInfo,
    })
  },
  hideCitySelected: function (e) {
    console.log(e)
    this.startAddressAnimation(false)
  },

  selectDistrict: function (e) {
    var that = this
    var ids = wx.getStorageSync('ids');
    wx.navigateTo({
      url: "/pages/yiguo/citys2/citys2?ids=" + ids,
    })
  },

  // 处理省市县联动逻辑
  cityChange: function (e) {
    var value = e.detail.value
    var provinces = this.data.provinces
    var citys = this.data.citys
    var areas = this.data.areas
    var provinceNum = value[0]
    var cityNum = value[1]
    var countyNum = value[2]
    if (this.data.value[0] != provinceNum) {
      var id = provinces[provinceNum].id
      this.setData({
        value: [provinceNum, 0, 0],
        citys: address.citys[id],
        areas: address.areas[address.citys[id][0].id],
      })
    } else if (this.data.value[1] != cityNum) {
      var id = citys[cityNum].id
      this.setData({
        value: [provinceNum, cityNum, 0],
        areas: address.areas[citys[cityNum].id],
      })
    } else {
      this.setData({
        value: [provinceNum, cityNum, countyNum]
      })
    }
  },

  //删除地址
  dels: function (e) {
    var id = e.target.dataset.id;
    var that = this;
    var uid = app.whereId.uid;
    wx.showModal({
      title: '提示',
      content: '确定要删除这个地址吗',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: 'https://www.cakeyouxuan.com/small/my/add_del',
            data: {
              id: id,
            },
            method: "POST",
            header: {
              "Content-Type": "application/x-www-form-urlencoded"  //post
            },
            success: function (res) {
              if (res.data.msg == 'T'){
                wx.showToast({
                  title: '删除成功',
                  icon: 'success',
                  duration: 2000,
                  success: function () {
                    setTimeout(function () {
                      //要延时执行的代码
                      wx.redirectTo({
                        url: "/pages/yiguo/address/address?ids=" + uid,
                       })
                    }, 2000) //延迟时间
                  }
                })  
                
              }
            },
          })
        } else if (res.cancel) {
          
        }
      }
    })  
  },

  //修改地址
  formSubmit:function(e){
    var uid = app.whereId.uid;
    var name = e.detail.value.name;
    var id = e.detail.value.id;
    var address = e.detail.value.address;
    var phone = e.detail.value.phone;
    var sheng = e.detail.value.sheng;
    wx.showModal({
      title: '提示',
      content: '确定要修改这个地址吗',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: 'https://www.cakeyouxuan.com/small/my/add_edit',
            data: {
              id: id,
              name: name,
              address: address,
              phone: phone,
              sheng:sheng,
            },
            method: "POST",
            header: {
              "Content-Type": "application/x-www-form-urlencoded"  //post
            },
            success: function (res) {
              if (res.data.msg == 'T') {
                wx.showToast({
                  title: '修改成功',
                  icon: 'success',
                  duration: 2000,
                  success: function () {
                    setTimeout(function () {
                      //要延时执行的代码
                      wx.redirectTo({
                        url: "/pages/yiguo/address/address?ids=" + uid,
                      })
                    }, 2000) //延迟时间
                  }
                })

              }
            },
          })
        } else if (res.cancel) {

        }
      }
    }) 
  }

})




